'use strict';

/* Controllers */
app.controller('houseOwnerIndexCtrl', function($scope, $http, rest, $compile, $location, ngDialog, user, $state, $window, uiLoad, $rootScope, $ocLazyLoad, $timeout) {
    $()
    var token = $window.localStorage.getItem("token");
    var userId = $window.localStorage.getItem("id");
    var listLandlordId = $window.localStorage.getItem('landlordId1');

    //方法级权限
    var accessStr = $window.localStorage.getItem("accessStr");

    $scope.params = {
        homeDateCode: "01"
    }

    rest.decoJava({
        type: "post",
        url: "/tenancy-sys/home-resource/count/config",
        data: "",
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        success: function(data) {
            $scope.provinceList = data.result;
        }
    });

    rest.decoJava({
        type: "post",
        url: "/tenancy-sys/home-resource/count/house",
        data: $scope.params,
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        success: function(data) {
            $scope.totalHouse = data.result.totalHouse;
            var yesterday = $scope.totalHouse.countNumByYearDay;
            var dayBefore = $scope.totalHouse.countNumByBeforYearDay;
            if ((yesterday == 0) || (dayBefore == 0)) {
                $scope.housenum = 0
            } else if (yesterday - dayBefore >= 0) {
                $scope.housenum = (yesterday - dayBefore) / dayBefore;
                $scope.housenum = "+" + ($scope.housenum * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#FE3E14');
            } else {
                $scope.housenum = (yesterday - dayBefore) / dayBefore;
                $scope.housenum = "-" + ($scope.housenum * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#40AC51');
            }

            var whiteOrYearDay = $scope.totalHouse.countNumByWhiteOrYearDay;
            var whiteOrBeforYearDay = $scope.totalHouse.countNumByWhiteOrBeforYearDay;
            if ((whiteOrYearDay == 0) || (whiteOrBeforYearDay == 0)) {
                $scope.housenumWhite = 0
            } else if (whiteOrYearDay - whiteOrBeforYearDay >= 0) {
                $scope.housenumWhite = (whiteOrYearDay - whiteOrBeforYearDay) / whiteOrBeforYearDay;
                $scope.housenumWhite = "+" + ($scope.housenumWhite * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#FE3E14');
            } else {
                $scope.housenumWhite = (whiteOrYearDay - whiteOrBeforYearDay) / whiteOrBeforYearDay;
                $scope.housenumWhite = "-" + ($scope.housenumWhite * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#40AC51');
            }

            var dateLength = data.result._icoMap.date.length - 1;
            $scope.date = data.result._icoMap.date[dateLength];
            $scope._icoCountNum = data.result._icoCountNum;
            $scope._icoCountNumByFailed = data.result._icoCountNumByFailed;
            $scope._icoCountNumBySuccess = data.result._icoCountNumBySuccess;

            $scope.houseCountNumberWhite = $scope.totalHouse.countNumByWhiteOrYearDay;
            $scope.houseCount = $scope.totalHouse.countNumber;
            $scope.houseCountByWhite = $scope.totalHouse.countNumberByWhite;
            $scope.houseCountNumber = $scope.totalHouse.countNumByYearDay;
            console.log($scope.date);
            $scope.$apply();
        }
    });

    rest.decoJava({
        type: "post",
        url: "/tenancy-sys/home-resource/count/order",
        data: $scope.params,
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        success: function(data) {
            // $scope.totalHouse = data.result.totalHouse;
            var dateLength = data.result._icoMap.date.length - 1;
            $scope.date = data.result._icoMap.date[dateLength];
            $scope.totalOrder = data.result.totalOrder;

            $scope._icoCountNum1 = data.result._icoCountNum;
            $scope._icoCountNumByFailed1 = data.result._icoCountNumByFailed;
            $scope._icoCountNumBySuccess1 = data.result._icoCountNumBySuccess;

            $scope.creditLineTotal = $scope.totalOrder.creditLineTotal;
            $scope.decorateOrder = $scope.totalOrder.decorateOrder;
            $scope.decorateYes = $scope.totalOrder.decorateOrderByYesterDay;
            $scope.decorateBeforYes = $scope.totalOrder.decorateOrderByBeforYesterDay;

            if (($scope.decorateYes == 0) || ($scope.decorateBeforYes == 0)) {
                $scope.decorate = 0
            } else if ($scope.decorateYes - $scope.decorateBeforYes >= 0) {
                $scope.decorate = ($scope.decorateYes - $scope.decorateBeforYes) / $scope.decorateBeforYes;
                $scope.decorate = "+" + ($scope.decorate * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#FE3E14');
            } else {
                $scope.decorate = ($scope.decorateYes - $scope.decorateBeforYes) / $scope.decorateBeforYes;
                $scope.decorate = "-" + ($scope.decorate * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#40AC51');
            }

            console.log($scope.date);
            $scope.$apply();
        }
    });

    rest.decoJava({
        type: "post",
        url: "/tenancy-sys/home-resource/count/landlord",
        data: $scope.params,
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        success: function(data) {
            //$scope.totalHouse = data.result.totalHouse;
            $scope.landlordCountNum = data.result.totalLandlord.countNumber;
            $scope.landlordCountNumByWhite = data.result.totalLandlord.countNumberByWhite;
            $scope.landlordCountNumber = data.result.totalLandlord.countNumByYearDay;
            $scope.landlordCountNumberWhite = data.result.totalLandlord.countNumByWhiteOrYearDay;
            $scope.landlordCountWhiteBefor = data.result.totalLandlord.countNumByWhiteOrBeforYearDay;
            $scope.landlordCountWhiteYear = data.result.totalLandlord.countNumByBeforYearDay;

            var landlordOrYearDay = $scope.landlordCountNumberWhite;
            var landlordOrBeforYearDay = $scope.landlordCountWhiteBefor;

            if ((landlordOrYearDay == 0) || (landlordOrBeforYearDay == 0)) {
                $scope.landlordnumWhite = 0
            } else if (landlordOrYearDay - landlordOrBeforYearDay >= 0) {
                $scope.landlordnumWhite = (landlordOrYearDay - landlordOrBeforYearDay) / landlordOrBeforYearDay;
                $scope.landlordnumWhite = "+" + ($scope.landlordnumWhite * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#FE3E14');
            } else {
                $scope.landlordnumWhite = (landlordOrYearDay - landlordOrBeforYearDay) / landlordOrBeforYearDay;
                $scope.landlordnumWhite = "-" + ($scope.landlordnumWhite * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#40AC51');
            }

            var landlordCountOrYearDay = $scope.landlordCountNumberWhite;
            var landlordCountOrBeforYearDay = $scope.landlordCountWhiteBefor;
            if ((landlordCountOrYearDay == 0) || (landlordCountOrBeforYearDay == 0)) {
                $scope.landlordnum = 0
            } else if (landlordCountOrYearDay - landlordCountOrBeforYearDay >= 0) {
                $scope.landlordnum = (landlordCountOrYearDay - landlordCountOrBeforYearDay) / landlordCountOrBeforYearDay;
                $scope.landlordnum = "+" + ($scope.landlordnum * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#FE3E14');
            } else {
                $scope.landlordnum = (landlordCountOrYearDay - landlordCountOrBeforYearDay) / landlordCountOrBeforYearDay;
                $scope.landlordnum = "-" + ($scope.landlordnum * 100).toFixed(2) + "%";
                $('.home-sj').css('color', '#40AC51');
            }

            console.log($scope.date);
            $scope.$apply();
        }
    });

    rest.decoJava({
        type: "post",
        url: "/tenancy-sys/home-resource/count/countYFOrderInfo",
        data: $scope.params,
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        success: function(data) {
            //$scope.totalHouse = data.result.totalHouse;
            $scope.$apply(function() {
                $scope.orderInfo = data.result._icoCountNum;
                $scope.orderInfoSuccess = data.result._icoCountNumBySuccess;
                $scope.orderInfoFailed = data.result._icoCountNumByFailed;
                $scope.orderInfoYesterday = data.result.yesterdayLoan;
                $scope.orderInfobefore = data.result.beforeYesterdayLoan;

                var orderInfoYesterday = $scope.orderInfoYesterday;
                var orderInfobefore = $scope.orderInfobefore;

                if (data.result.yesterdayLoan == null) {
                    $scope.orderInfoYesterday = 0;
                }

                if ((orderInfoYesterday == 0) || (orderInfobefore == 0)) {
                    $scope.orderInfoNumber = 0
                } else if (orderInfoYesterday - orderInfobefore >= 0 && orderInfoYesterday != null && orderInfobefore != null) {
                    $scope.orderInfoNumber = (orderInfoYesterday - orderInfobefore) / orderInfoYesterday;
                    $scope.orderInfoNumber = "+" + ($scope.orderInfoNumber * 100).toFixed(2) + "%";
                    $('.home-sj').css('color', '#FE3E14');
                } else if(orderInfoYesterday != null && orderInfobefore != null){
                    $scope.orderInfoNumber = (orderInfoYesterday - orderInfobefore) / orderInfoYesterday;
                    $scope.orderInfoNumber = "-" + ($scope.orderInfoNumber * 100).toFixed(2) + "%";
                    $('.home-sj').css('color', '#40AC51');
                }else{
                    $scope.orderInfoNumber = '0';
                }
            });
            console.log($scope.date);

        }
    });


    $scope.changeHouseDate = function(number) {
        rest.decoJava({
            type: "post",
            url: "/tenancy-sys/home-resource/count/house",
            data: {
                homeDateCode: number
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            success: function(data) {
                // $scope.totalHouse = data.result.totalHouse;
                var icoMapDate = data.result._icoMap.date;
                var houseCount = data.result._icoMap.houseCount;
                var houseCountSuccess = data.result._icoMap.houseCountSuccess;
                var houseCountfalied = data.result._icoMap.houseCountfalied;
                var dateLength = data.result._icoMap.date.length - 1;
                $scope.date = data.result._icoMap.date[dateLength];
                $scope._icoCountNum = data.result._icoCountNum;
                $scope._icoCountNumByFailed = data.result._icoCountNumByFailed;
                $scope._icoCountNumBySuccess = data.result._icoCountNumBySuccess;


                console.log($scope.date);
                $scope.$apply();

                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('main-echart'));

                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: ''
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['提交房源数', '授信成功数', '授信拒绝数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: ''
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: icoMapDate
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '提交房源数',
                        type: 'line',
                        stack: '总量',
                        data: houseCount
                    }, {
                        name: '授信成功数',
                        type: 'line',
                        stack: '总量',
                        data: houseCountSuccess
                    }, {
                        name: '授信拒绝数',
                        type: 'line',
                        stack: '总量',
                        data: houseCountfalied
                    }]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

            }
        });
    }

    $scope.changeHouseDate('03')

    $scope.changeMonthDate = function(number) {
        rest.decoJava({
            type: "post",
            url: "/tenancy-sys/home-resource/count/countYFOrderInfo",
            data: {
                homeDateCode: number
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            success: function(data) {
                //$scope.totalHouse = data.result.totalHouse;
                //var dateLength = data.result._icoMap.date.length - 1;
                //$scope.date = data.result._icoMap.date[dateLength];
                //$scope._icoMap = data.result._icoMap;
                //var lng = data.result._icoMap.length;

                //$scope._icoMap = data.result._icoMap.substring(1,lng-1);
                // $scope._icoMap = $scope._icoMap.replace(/ /g,"");
                // $scope._icoMap = $scope._icoMap.split(",");
                // console.log("222222222",$scope._icoMap);
                // var resData = $scope._icoMap;
                // var myRes = [];
                // var myJson = {};
                // for(var i=0; i<resData.length; i++){
                //     var leng = resData[i].split('=')[1].length
                //     myJson[resData[i].split('=')[0]] = resData[i].split('=')[1].substring(1,leng-1);
                //  myRes.push(myJson);
                // }
                // $scope._icoMap = myJson;

                // console.log("111111",$scope._icoMap)
                var OrderNumDate = data.result._icoMap.date;
                var OrderNum = data.result._icoMap.icoCountNum;
                var OrderNumSuccess = data.result._icoMap.icoCountNumBySuccess;
                var OrderNumFailed = data.result._icoMap.icoCountNumByFailed;

                $scope.icoOrderNum = data.result._icoCountNum;
                $scope.icoOrderNumFailed = data.result._icoCountNumByFailed;
                $scope.icoOrderNumSuccess = data.result._icoCountNumBySuccess;

                $scope.$apply();

                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('main-echart3'));

                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: ''
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['提交订单数', '放款笔数', '拒绝笔数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: ''
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: OrderNumDate
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '提交订单数',
                        type: 'line',
                        stack: '总量',
                        data: OrderNum
                    }, {
                        name: '放款笔数',
                        type: 'line',
                        stack: '总量',
                        data: OrderNumSuccess
                    }, {
                        name: '拒绝笔数',
                        type: 'line',
                        stack: '总量',
                        data: OrderNumFailed
                    }]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

            }
        });
    }

    $scope.changeMonthDate('03')

    $scope.changeDecorateDate = function(number) {
        rest.decoJava({
            type: "post",
            url: "/tenancy-sys/home-resource/count/order",
            data: {
                homeDateCode: number
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            success: function(data) {
                // $scope.totalHouse = data.result.totalHouse;
                var icoMapDateMonth = data.result._icoMap.date;
                var decorateTotal = data.result._icoMap.decorateTotal;
                var decorateLoanResult = data.result._icoMap.decorateLoanResult;
                var decorateLoanResultFalied = data.result._icoMap.decorateLoanResultFalied;
                var dateLength = data.result._icoMap.date.length - 1;
                $scope.date = data.result._icoMap.date[dateLength];

                $scope._icoCountNum1 = data.result._icoCountNum;
                $scope._icoCountNumByFailed1 = data.result._icoCountNumByFailed;
                $scope._icoCountNumBySuccess1 = data.result._icoCountNumBySuccess;

                $scope.num = $scope._icoCountNumBySuccess1 / $scope._icoCountNum1;
                $scope.num = ($scope.num * 100).toFixed(0) + "%";

                $scope.nonum = $scope._icoCountNumByFailed1 / $scope._icoCountNum1;
                $scope.nonum = ($scope.nonum * 100).toFixed(0) + "%";



                console.log($scope.num);
                console.log($scope.nonum);
                $scope.$apply();

                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('main-echart2'));

                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: ''
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['提交订单数', '放款笔数', '拒绝笔数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: ''
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: icoMapDateMonth
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '提交订单数',
                        type: 'line',
                        stack: '总量',
                        data: decorateTotal
                    }, {
                        name: '放款笔数',
                        type: 'line',
                        stack: '总量',
                        data: decorateLoanResult
                    }, {
                        name: '拒绝笔数',
                        type: 'line',
                        stack: '总量',
                        data: decorateLoanResultFalied
                    }]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
            }
        });
    }
    $scope.changeDecorateDate('03')


    $(document).ready(function() {
        $(".home-click-ul1 li").each(function(index) {
            $(this).click(function() {
                $(".home-click-ul1 li").removeClass("now");
                $(".home-click-ul1 li").eq(index).addClass("now");
            });
        });
    });
    $(document).ready(function() {
        $(".home-click-ul2 li").each(function(index) {
            $(this).click(function() {
                $(".home-click-ul2 li").removeClass("now");
                $(".home-click-ul2 li").eq(index).addClass("now");
            });
        });
    });
    $(document).ready(function() {
        $(".home-click-ul3 li").each(function(index) {
            $(this).click(function() {
                $(".home-click-ul3 li").removeClass("now");
                $(".home-click-ul3 li").eq(index).addClass("now");
            });
        });
    });

});